/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pracamagisterska;

import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import javax.swing.JComponent;
import javax.swing.JTree;
import javax.swing.event.TreeModelListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;

/**
 *
 * @author Adrian
 */
public class CardSearch extends ModalityInternalFrame {

    private DatabaseThread db;
    private DefaultMutableTreeNode root;
    private JTree tree;

    /**
     * Creates new form CardSearch
     */
    public CardSearch(JComponent parent, DatabaseThread db) {
        super(parent, db);
        this.db = db;
        initComponents();
        createTree();
    }

    private void createTree() {
        DateFormat dateFormat = new SimpleDateFormat("yyyy");
        Date date = new Date();
        Integer curdate = Integer.parseInt(dateFormat.format(date));
        ArrayList<String> dates = new ArrayList<String>();
        root = new DefaultMutableTreeNode("Karty meldunkowe");
        tree = new JTree(root);
        for (int i = 2010; i <= curdate + 1; i++) {
            dates.add(i + "");
            DefaultMutableTreeNode x = new DefaultMutableTreeNode(i);
            root.add(x);
            RefillCardNode(root, i + "", dates.indexOf(i + ""));
        }

        jScrollPane1.setViewportView(tree);
    }

    private void RefillCardNode(DefaultMutableTreeNode root, String date, int index) {
        try {
            DefaultListModel list = db.Review_ListRefill(date);
            addNode((DefaultMutableTreeNode) root.getChildAt(index), list);
        } catch (SQLException ex) {
            Logger.getLogger(CardSearch.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void RefillPersonNode(int index,DefaultMutableTreeNode parent) {
        try {
            ArrayList<ReviewList> l = db.Review_PersonListRefill(index);
            for(int i=1;i<l.size();i++){
                parent.add(new DefaultMutableTreeNode(l.get(i).toString()));
            }
        } catch (SQLException ex) {
            Logger.getLogger(CardSearch.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    private void addNode(DefaultMutableTreeNode parent, DefaultListModel leafs) {
        DefaultMutableTreeNode p;
        if (leafs.size() != 0) {
            for (int i = 0; i < leafs.size(); i++) {
                    parent.add(new DefaultMutableTreeNode(leafs.get(i)));
                    RefillPersonNode(i,(DefaultMutableTreeNode) parent.getChildAt(i));                
            }
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTree1 = new javax.swing.JTree();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        jScrollPane1.setViewportView(jTree1);

        jButton1.setText("Zobacz szczegóły");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Zamknij");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 350, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(30, 30, 30)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 145, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(22, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 423, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 130, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        System.out.println(root.getIndex((TreeNode) tree.getLastSelectedPathComponent()));
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        dispose();
    }//GEN-LAST:event_jButton2ActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTree jTree1;
    // End of variables declaration//GEN-END:variables
}
